package jdbc;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Properties;

/**
 * 德鲁伊连接池
 */
public class Druid_ {

    public static void main(String[] args) throws Exception {
        //加入配置文件 druid.properties 拷贝到src目录下
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\druid.properties"));

        //1.创建一个指定参数的数据库连接池 Druid连接池
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        long start = System.currentTimeMillis();
        for (int i = 0; i < 500000; i++) {
            Connection connection = dataSource.getConnection();
            System.out.println(connection.getClass());
            //System.out.println("连接成功!");
            connection.close();
        }
        long end = System.currentTimeMillis();
        //druid 连接池 操作50000 耗时=1854
        System.out.println("druid 连接池 操作 500000 耗时=" + (end- start));//1854
    }
}
